/*
 * Copyright (C) 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.ros.android.view.visualization.layer;

import org.ros.android.view.visualization.VisualizationView;
import org.ros.android.view.visualization.shape.GoalShape;
import org.ros.android.view.visualization.shape.Shape;
import org.ros.message.MessageListener;
import org.ros.namespace.GraphName;
import org.ros.node.ConnectedNode;
import org.ros.rosjava_geometry.FrameTransform;
import org.ros.rosjava_geometry.Transform;

import javax.microedition.khronos.opengles.GL10;

import geometry_msgs.PoseWithCovarianceStamped;

/**
 * @author moesenle@google.com (Lorenz Moesenlechner)
 */
public class MyRobotShowLayer_Cartographer_pose extends SubscriberLayer<geometry_msgs.PoseStamped> implements
    TfLayer {

  private final GraphName targetFrame;

  private Shape shape;
  private boolean ready;
  VisualizationView my_view;
  public MyRobotShowLayer_Cartographer_pose(VisualizationView view,String topic) {
    this(GraphName.of(topic));
    shape = new GoalShape();
    my_view=view;
    ready = false;
  }

  public MyRobotShowLayer_Cartographer_pose(GraphName topic) {
    super(topic, geometry_msgs.PoseStamped._TYPE);
    targetFrame = GraphName.of("map");
    ready = false;
  }

  @Override
  public void draw(VisualizationView view, GL10 gl) {
    if (ready) {
      shape.draw(view, gl);
    }
  }


  public void setMyRobotPost( PoseWithCovarianceStamped mypost_message) {
    GraphName source = GraphName.of(mypost_message.getHeader().getFrameId());
    FrameTransform frameTransform = my_view.getFrameTransformTree().transform(source, targetFrame);
    if (frameTransform != null) {
        ready = true;
        Transform poseTransform = Transform.fromPoseMessage(mypost_message.getPose().getPose());
        shape.setTransform(frameTransform.getTransform().multiply(poseTransform));
    }
  }



  @Override
  public void onStart(final VisualizationView view, ConnectedNode connectedNode) {
    super.onStart(view, connectedNode);
  }

  @Override
  public GraphName getFrame() {
    return targetFrame;
  }
}